package com.jeesite.modules.isys.common.aop.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.jeesite.common.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

//处理字符串为空情况
@Component
public class NullExec {


    //在查询器场景下需要构建
    public Object execStrNull(Object object){
        if (object instanceof  String){
           if(StrUtil.isBlank(String.valueOf(object))){
               return null;
           }

           if ("".equals(String.valueOf(object))){
               return null;
           }
        }

        return object;
    }



    public Object execObjNull(Object object) throws IllegalAccessException {

        if (object == null){
            return  null;
        }
        TableName annotation = object.getClass().getAnnotation(TableName.class);

        if (annotation!=null) {
            if (object instanceof String) {
                object = execStrNull(object);
            } else {
                //按普通对象处理
                Field[] declaredFields = object.getClass().getDeclaredFields();
                for (Field field : declaredFields) {
                    field.setAccessible(true);

                    //判断类型
                    if (field.getType() == String.class) {
                        Object execked = execStrNull(field.get(object));
                        field.set(object, execked);
                    }
                }
            }
        }
        return  object;
    }
}
